const express = require('express')
const jwt = require('../../config/jwt')

const router =  express.Router()
const studentModel = require('../../model/login/student')
let result = {
    data: '',
    msg: '',
    code: 200
}

// 学生注册用户信息
router.post('/register',(request,response)=>{
    console.log(request.body);
    const { userid, name, phone, password,email, sex,bankcard,className} = request.body
    console.log(  userid, name, phone, password,email, sex,bankcard,className);
    studentModel.create({
        userid, name, phone, password,email, sex,bankcard,className,userhead: '/imgs/head.jpg'
    }).then(res=>{
        let result = {
            data: res,
            msg: '成功',
            code: 200
        }
        response.json(result)
    }).catch(err=>{
        let result = {
            data: err,
            msg: '失败',
            code: 2001
        }
        console.log(err,'err');
        response.json(result)
    })
})

// 学生登录接口(可通过邮箱、手机号码、用户名登录)
router.post('/login',(request,response)=>{
    const {username,password} = request.body
    console.log(username,password);
    studentModel.findOne({$or: [{name:username},{userid:username},{email:username},{phone:username}]}).then(res=>{
        // console.log(res);
        if(res){
            const token = jwt('set',{username,password})
            // jwt('get',token)
            console.log(token);
            result.data = res
            result.token = token
            result.msg = '登录成功'
        }else{
            result.data = res
            result.msg = '没有这个用户'
        }
        response.json(result)
    }).catch(err=>{
        result.data = err
        result.msg = '出错拉'
        response.json(result)
    })
})

router.delete('/delUser',async (request,response)=>{
    console.log(request.query.userid);
    let resultDate = await studentModel.deleteOne({userid: request.query.userid})
    console.log('resultDate',resultDate);
    result.data = resultDate
    response.json(result)
})



module.exports = router